package com.csthink.common.infrastructure.util;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.MessageSource;
import org.springframework.context.NoSuchMessageException;
import org.springframework.context.i18n.LocaleContextHolder;

/**
 * Error message util
 *
 * @author <a href="mailto:security.2009@live.cn">Mars</a>
 * @since 2023-09-09
 */
@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ErrorMessageUtil {

    public static String getMessage(int errorCode, String... args) {

        try {
            return SpringUtil.getBean(MessageSource.class).getMessage(String.valueOf(errorCode),
                    args, LocaleContextHolder.getLocale());
        } catch (NoSuchMessageException e) {
            log.error("No such message source: {}", errorCode);
            return "";
        }
    }
}
